package com.linksfield.topup.controller.login;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.linksfield.topup.config.dto.ResultDataDto;
import com.linksfield.topup.config.dto.StringOutputConstant;
import com.linksfield.topup.entity.interfaces.login.request.RequestConfirmChangePwd;
import com.linksfield.topup.entity.interfaces.login.request.RequestLogin;
import com.linksfield.topup.entity.interfaces.login.response.ResponseClickChangePwd;
import com.linksfield.topup.entity.interfaces.login.response.ResponseLogin;
import com.linksfield.topup.service.SysOperateService;
import com.linksfield.topup.utils.MD5;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author: lm
 * @date: 2019-11-21 15:40
 */
@RestController
@Api(value = "系统登录")

public class LoginController {

    private static Logger logger = Logger.getLogger(LoginController.class);

    @Autowired
    SysOperateService operateService;

    @CrossOrigin
    @PostMapping(value = "login", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "登录", response = ResponseLogin.class)
    public ResultDataDto login(@ApiParam @RequestBody RequestLogin request) {

        if (null == request) {
            logger.info("请求内容为空");
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_PARAM_ERROR);
        }

        if (StringUtils.isBlank(JSON.toJSONString(request))) {
            logger.info("请求报文不合法:requestStr为空");
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_PARAM_ERROR);
        }

        String requestStr = JSON.toJSONString(request);
        logger.info("前端请求是：" + requestStr);

        if (null == JSONArray.parseObject(requestStr, RequestLogin.class)) {
            logger.info("请求报文不合法:请求报文可能是{}");
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_PARAM_ERROR);
        }

        RequestLogin re = JSONArray.parseObject(requestStr, RequestLogin.class);
        if (StringUtils.isBlank(re.getAccount()) || StringUtils.isBlank(re.getPassword()) || StringUtils.isBlank(re.getLang()) ||
                StringUtils.isBlank(re.getVersion()) || StringUtils.isBlank(re.getTradeType())) {
            logger.info("请求报文不合法:存在不允许为空的参数");
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_PARAM_ERROR);
        }

        String account = re.getAccount();
        String password = MD5.calcMD5(re.getPassword());
        return operateService.checkLogin(account, password);
    }

    @CrossOrigin
    @PostMapping(value = "logout", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "退出")
    public ResultDataDto loginOut(@RequestHeader(name = "x-access-token", required = false) String token) {

        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        return operateService.checkLogout(token);
    }

    @CrossOrigin
    @PostMapping(value = "clickChangePwd", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "打开修改密码", response = ResponseClickChangePwd.class)
    public ResultDataDto clickChangePwd(@RequestHeader(name = "x-access-token", required = false) String token) {

        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        return operateService.clickChangePwd(token);
    }

    @CrossOrigin
    @PostMapping(value = "confirmChangePwd", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "确认修改密码")
    public ResultDataDto confirmChangePwd(@ApiParam @RequestBody RequestConfirmChangePwd request,
                                          @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【确认修改密码】请求：" + JSON.toJSONString(request));

        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        return operateService.confirmChangePwd(request, token);
    }

}

